package com.wx.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wx.entity.SmbmsBill;
import com.wx.entity.SmbmsProvider;
import com.wx.mapper.SmbmsBillMapper;
import com.wx.mapper.SmbmsProviderMapper;
import com.wx.service.SmbmsBillService;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * @author wangxiao
 * @since 2023-03-20 13:56:51
 */
//@Slf4j
@Service
//@RequiredArgsConstructor
//@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SmbmsBillServiceImpl implements SmbmsBillService {

    @Autowired
    private SmbmsBillMapper smbmsBillMapper;

    @Override
    public void addSmbmsBill(SmbmsBill smbmsBill) {
        smbmsBillMapper.insert(smbmsBill);
    }

    @Override
    public void delSmbmsBill(String id) {
        smbmsBillMapper.deleteById(id);
    }

    @Override
    public void updSmbmsBill(SmbmsBill smbmsBill) {
        smbmsBillMapper.updateById(smbmsBill);
    }

    @Override
    public SmbmsBill getSmbmsBillById(String id) {
        SmbmsBill smbmsBill =  smbmsBillMapper.selectById(id);
        SmbmsProvider smbmsProvider = smbmsProviderMapper.selectById(smbmsBill.getProviderId());
        smbmsBill.setProviderName(smbmsProvider.getProName());
        return smbmsBill;
    }

    @Autowired
    SmbmsProviderMapper smbmsProviderMapper;

    @Override
    public IPage<SmbmsBill> getSmbmsBillByPage(Page page, SmbmsBill smbmsBill) {
        LambdaQueryWrapper<SmbmsBill> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(smbmsBill.getProductName())){
            lambdaQueryWrapper.like(SmbmsBill::getProductName,smbmsBill.getProductName());
        }
        IPage<SmbmsBill> smbmsBillIPage = smbmsBillMapper.selectPage(page, lambdaQueryWrapper);
        smbmsBillIPage.getRecords().forEach(smbmsBillDb -> {
            SmbmsProvider smbmsProvider = smbmsProviderMapper.selectById(smbmsBillDb.getProviderId());
            smbmsBillDb.setProviderName(smbmsProvider.getProName());
        });
        return smbmsBillIPage;
    }


}